package com.dxq.test.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import androidx.annotation.NonNull;

import com.dxq.test.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Author: zsh
 * Date: 2023/11/14
 * Describe: 工具类
 */
public class MyUtil {

    private static SimpleDateFormat getDefaultFormat() {
        String format = "yyyyMMddHHmmssSSS";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat;
    }

    public static String date2String(final Date date) {
        return date2String(date, getDefaultFormat());
    }

    public static String date2String(final Date date, @NonNull final DateFormat format) {
        return format.format(date);
    }

    /**
     * dialog提示--有标题，两个按钮，有回调
     *
     * @param activity            当前调用的activity
     * @param themeResId          主题
     * @param title               标题
     * @param msg                 内容
     * @param button1             按钮1
     * @param button2             按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, int themeResId, String title, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setTitle(title);
        builder.setIcon(R.drawable.ic_warning_24);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onNegativeClick();
                dialog.dismiss();
            }
        });
        //按钮2（确定）
        builder.setPositiveButton(button2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--有标题，两个按钮，有回调
     *
     * @param activity            当前调用的activity
     * @param title               标题
     * @param msg                 内容
     * @param button1             按钮1
     * @param button2             按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, String title, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        normalDialog(activity, 0, title, msg, button1, button2, dialogClickListener);
    }

    public static void normalDialog(Activity activity, String title, String msg, final DialogClickListener dialogClickListener) {
        normalDialog(activity, title, msg, "取消", "确定", dialogClickListener);
    }

    /**
     * dialog提示--有标题，一个按钮，有回调
     *
     * @param activity            当前调用的activity
     * @param themeResId          主题
     * @param title               标题
     * @param msg                 内容
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, int themeResId, String title, String msg,
                                    final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setTitle(title);
        builder.setIcon(R.drawable.ic_warning_24);
        builder.setMessage(msg);
        //按钮（确定）
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (null != dialogClickListener)
                    dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    public static void normalDialogOne(Activity activity, String title, String msg, final DialogClickListener dialogClickListener) {
        normalDialog(activity, 0, title, msg, dialogClickListener);
    }

    public static void normalDialogOne(Activity activity, String title, String msg) {
        normalDialog(activity, 0, title, msg, null);
    }

    /**
     * 列表对话框
     */
    public static void itemListDialog(Activity activity, String title, String[] lesson,
                                      final DialogListClickListener dialogListClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title);
        builder.setCancelable(true);
        //builder.setIcon(R.mipmap.ic_launcher);
        builder.setItems(lesson, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogListClickListener.onClick(which);
            }
        }).create();
        //设置反面按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();     //创建AlertDialog对象
        dialog.show();                              //显示对话框
    }
}
